package org.university.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.university.entity.Classroom;
import org.university.repository.ClassroomRepository;
import org.university.service.ClassroomService;

@Service
public class ClassroomServiceImpl extends GenericServiceImpl<Classroom, Integer> implements ClassroomService {

    private static final Logger logger = LoggerFactory.getLogger(ClassroomServiceImpl.class);
    private final ClassroomRepository classroomRepository;

    public ClassroomServiceImpl(ClassroomRepository repository, ClassroomRepository classroomRepository) {
        super(repository);
        this.classroomRepository = classroomRepository;
    }

    @Override
    public Classroom save(Classroom classroom) {
        Integer id = classroom.getClassroomId();
        logger.info("保存教室ID: {}", id);
        if (id != null && classroomRepository.existsById(id)) {
            throw new IllegalArgumentException("教室ID已存在: " + id);
        }
        return classroomRepository.save(classroom);
    }

    @Override
    public Page<Classroom> findAll(Pageable pageable) {
        logger.info("查询所有教室: 分页信息={}", pageable);
        return classroomRepository.findByDeletedFalse(pageable);
    }

    @Override
    public Page<Classroom> findAll() {
        logger.info("查询所有教室（无分页）");
        return classroomRepository.findByDeletedFalse(Pageable.unpaged());
    }

    @Override
    public void deleteById(Integer id) {
        logger.info("删除教室ID: {}", id);
        classroomRepository.findById(id).ifPresent(classroom -> {
            classroom.setDeleted(1);
            classroomRepository.save(classroom);
        });
    }
}